﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace HQWeb.SysManage.DictionaryManage
{
    public partial class DictionaryManage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                BindType();
                string ID = Request.QueryString["ID"];
                ParentID.Value = Request.QueryString["ID"];
                if (ID == null)
                {
                    this.btn_Update.Visible = false;
                    btn_Delete.Visible = false;
                }
                else
                {
                    int Did = int.Parse(ID);
                    if (Did >= 1 && Did <= 15)
                    {
                        btn_Delete.Visible = false;
                    }
                    this.LoadData(ID);
                } 
            }
        }

        public void LoadData(string ID)
        {
            int Did = int.Parse(ID);
            BLL.SysManage.T_PUB_DICTIONARY option = new BLL.SysManage.T_PUB_DICTIONARY();
            Model.SysManage.T_PUB_DICTIONARY detail = option.GetModel(Did);

            this.txt_parentID.Value = detail.PARENT_ID.ToString();

            ddl_type.SelectedValue = detail.PARENT_ID.ToString();
            this.txt_Name.Text = detail.NAME;

            this.txt_OrderID.Text = detail.ORDER_ID.ToString();
            this.txt_Other.Text = detail.REMARK;

        }

        #region 绑定所属类型
        protected void BindType()
        {
            BLL.SysManage.T_PUB_DICTIONARY bll = new BLL.SysManage.T_PUB_DICTIONARY();
            string str = " FLAG = 1 and PARENT_ID = 0 ";
            IList il = bll.GetModelList(str);
            ddl_type.ClearSelection();
            ddl_type.DataSource = il;
            ddl_type.DataTextField = "NAME"; //dropdownlist的Text的字段 
            ddl_type.DataValueField = "ID";//dropdownlist的Value的字段 
            ddl_type.DataBind();
            ListItem item = new ListItem("", "0");
            item.Selected = true;
            ddl_type.Items.Add(item);
        }
        #endregion

        #region 修改用户组
        protected void btn_Update_Click(object sender, EventArgs e)
        {
            Model.SysManage.T_PUB_DICTIONARY detail = new Model.SysManage.T_PUB_DICTIONARY();

            BLL.SysManage.T_PUB_DICTIONARY option = new BLL.SysManage.T_PUB_DICTIONARY();

            int ID = int.Parse(ParentID.Value);
            detail = option.GetModel(ID);

            if (detail.PARENT_ID != 0)
            {
                detail.PARENT_ID = int.Parse(this.ddl_type.SelectedValue.Trim());
            }

            detail.NAME = this.txt_Name.Text.Trim();


            if (this.txt_OrderID.Text != "")
            {
                detail.ORDER_ID = Convert.ToInt32(this.txt_OrderID.Text);
            }
            else
            {
                detail.ORDER_ID = 0;
            }

            detail.REMARK = this.txt_Other.Text;



            if (option.GetModel(detail.ID).NAME != this.txt_Name.Text.Trim() && option.IsExistName(int.Parse(this.txt_parentID.Value.Trim()), this.txt_Name.Text.Trim()))
            {
                this.lbl_Message.Text = "此名称已存在";
                return;
            }
            else
            {
                this.lbl_Message.Text = "";
            }

            if (option.Update(detail))
            {
                Universal.JS.RunJS("alert('结点更新成功');window.parent.frames['leftFrame'].location.reload();");
            }
            else
            {
                Universal.JS.RunJS("修改失败");
            }
        }
        #endregion

        protected void btn_Delete_Click(object sender, EventArgs e)
        {
            Model.SysManage.T_PUB_DICTIONARY detail = new Model.SysManage.T_PUB_DICTIONARY();

            int ID = int.Parse(Request.QueryString["ID"]);

            BLL.SysManage.T_PUB_DICTIONARY Option = new BLL.SysManage.T_PUB_DICTIONARY();
            if (Option.DeleteByFlag(ID))
            {
                Universal.JS.RunJS("alert('结点删除成功');window.parent.frames['leftFrame'].location.reload();");
            }
            else
            {
                Universal.JS.RunJS("删除失败");
            }
        }
    }
}